import { RequestService } from './../service/service.service';
import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';


@Component({
  selector: 'app-article',
  templateUrl: './article.component.html',
  styleUrls: ['./article.component.scss']
})
export class ArticleComponent implements OnInit {
  visible: Boolean = false
  loading: Boolean = false;
  data = [
    {
      title: 'Ant Design Title 1',
      avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png',
      content: 'Lorem ipsum dolor sit amet consectetur adipisicing elit. At suscipit quaerat optio eligendi, sapiente quod error illum doloremque inventore tenetur ab, fugit, eaque voluptatibus est rem placeat. Illum, necessitatibus itaque.'
    },
    {
      title: 'Ant Design Title 2',
      avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png',
      content: 'Lorem ipsum dolor sit amet consectetur adipisicing elit. At suscipit quaerat optio eligendi, sapiente quod error illum doloremque inventore tenetur ab, fugit, eaque voluptatibus est rem placeat. Illum, necessitatibus itaque.'
    },
    {
      title: 'Ant Design Title 3',
      avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png',
      content: 'Lorem ipsum dolor sit amet consectetur adipisicing elit. At suscipit quaerat optio eligendi, sapiente quod error illum doloremque inventore tenetur ab, fugit, eaque voluptatibus est rem placeat. Illum, necessitatibus itaque.'
    },
    {
      title: 'Ant Design Title 4',
      avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png',
      content: 'Lorem ipsum dolor sit amet consectetur adipisicing elit. At suscipit quaerat optio eligendi, sapiente quod error illum doloremque inventore tenetur ab, fugit, eaque voluptatibus est rem placeat. Illum, necessitatibus itaque.'
    }
  ];
  pagination = {}
  title: String = 'article'
  params: Object = {
    page: 1,
    per_page: 1
  }
  constructor(private request: RequestService, private message: NzMessageService) { }

  ngOnInit() {
    this.request.get('/v1/article/list', this.params).subscribe(res => {
      // console.log(res)
      this.data = res['data']['list']
      Object.assign(this.pagination, res['data']['pagination'])
    })
  }

  change(): void {
    this.visible = true
  }
  closeModal(val) {
    this.visible = val
  }
  changePageIndex(pageIndex) {
    this.params['page'] = pageIndex
    this.ngOnInit()
  }
  changePageSize(pageSize) {
    this.params['per_page'] = pageSize
    this.ngOnInit()
  }
  confirmDelete(item) {
    this.request.delete(`/v1/article/${item._id}/delete`).subscribe(res => {
      if (res['code'] === 200) {
        this.ngOnInit()
      }
    })
  }

}
